package com.mk.sso.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.entity.sso.UserEntity;
import com.zxw.common.entity.sso.UserListQuery;
import com.zxw.common.entity.sso.UserQuery;
import com.zxw.common.entity.sso.UserVO;
import com.zxw.common.enums.ResultCode;
import com.mk.sso.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 用户表接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-11-25
 */
@Api(tags = "用户表接口", description = "UserController")
@RestController
@RequestMapping("/userEntity")
public class UserController {

	@Autowired
	private UserService userService;

	@ApiOperation("添加")
	@PostMapping("/save")
	public ApiResult<ResultCode> save(@RequestBody UserQuery userQuery) {
		userService.save(userQuery);
		return ApiResult.success(ResultCode.SUCCESS);
	}

	@ApiOperation("列表条件分页查询")
	@GetMapping("/list")
	public ApiResult<PageResult<UserVO>> list(UserListQuery userListQuery) {
		IPage<UserEntity> page = userService.list(userListQuery);
		List<UserVO> list = page.getRecords().stream().map(item -> {
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(item, userVO);
			return userVO;
		}).collect(Collectors.toList());
		return ApiResult.success(PageResult.restPage(page, list));
	}

	@ApiOperation("批量添加")
	@PostMapping("/saveBatch")
	public ApiResult<ResultCode> saveBatch(@RequestBody List<UserQuery> list) {
		userService.saveBatch(list);
		return ApiResult.success(ResultCode.SUCCESS);
	}

	@ApiOperation("修改")
	@PostMapping("/update")
	public ApiResult<ResultCode> update(@RequestBody UserQuery userQuery) {
		userService.updateById(userQuery);
		return ApiResult.success(ResultCode.SUCCESS);
	}

	@ApiOperation("批量删除")
	@PostMapping("/deletes")
	public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
		userService.removeByIds(ids);
		return ApiResult.success(ResultCode.SUCCESS);
	}

	@ApiOperation("详情")
	@GetMapping("/detail/{id}")
	public ApiResult<UserVO> detail(@PathVariable("id") String id) {
		UserEntity userEntity = userService.getById(id);
		UserVO userVO = new UserVO();
		BeanUtils.copyProperties(userEntity, userVO);
		return ApiResult.success(userVO);
	}

}

